package com.rock.code.provider.inter.controller;

import com.rock.code.base.controller.BaseController;
import com.rock.code.common.comm.DesEncrypterIsmp;
import com.rock.code.provider.inter.service.ProviderInterfaceService;
import com.rock.code.system.user.domain.TbSysLoginmsg;
import com.rock.code.system.user.service.TbSysLoginmsgService;
import net.sf.json.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.sql.Timestamp;
import java.util.*;

/**
 * @author zhangli
 * @version V1.0
 * @create 2019-04-12 16:48
 **/
@Controller
@RequestMapping("/providerInter")
public class ProviderInterfaceController extends BaseController {

    @Resource
    private ProviderInterfaceService providerInterfaceService;

    @Resource
    private TbSysLoginmsgService tbSysLoginmsgService;

    /**
     * @Author zhangli
     * @Description //TODO 获取指定IP
     * @Date 10:45 2019/4/14
     * @Param [IP]
     * @return java.lang.Object
     **/
    @ResponseBody
    @RequestMapping(value = "/queryBaseInfoFromKPIDETAIL", method = RequestMethod.GET)
    public Object queryBaseInfoFromKPIDETAIL(@RequestParam String IP) throws Exception {
        if (null == IP || StringUtils.isBlank(IP)) {
            return null;
        }

        //验证 是否有权限
        if(visit(request).equals("0000")){
            JSONObject json = new JSONObject();
            json.put("error","unauthorized");
            json.put("error_description","Full unauthorized is required  to assess this resource");
            ajaxJson.setSuccess(false);
            ajaxJson.setMsg("验证失败");
            ajaxJson.setResult(json);
            return ajaxJson;
        }
        Map map = new HashMap();
        map.put("IP", IP.replaceAll("\\.", "_"));

        List list = providerInterfaceService.queryBaseInfoFromKPIDETAIL(map);

        return list;

    }

    /***
     * @Author zhangli
     * @Description //TODO  获取IP下的所有指标列表实例
     * @Date 10:44 2019/4/14
     * @Param [requestBody] IP
     * @return java.lang.Object
     **/
    @ResponseBody
    @RequestMapping(value = "/queryKpiList", method = RequestMethod.POST)
    public Object queryKpiList(@RequestBody String requestBody) throws Exception {
        JSONObject jsonObject = JSONObject.fromObject(requestBody);
        Map<String, Object> param = this.getRequestMapSingle();
        String IP = String.valueOf(jsonObject.get("IP"));
        if (null == IP || StringUtils.isBlank(IP)) {
            return null;
        }
        //验证 是否有权限
        if(visit(request).equals("0000")){
            JSONObject json = json = new JSONObject();
            json.put("error","unauthorized");
            json.put("error_description","Full unauthorized is required  to assess this resource");
            ajaxJson.setSuccess(false);
            ajaxJson.setMsg("验证失败");
            ajaxJson.setResult(json);
            return ajaxJson;
        }

        Map map = new HashMap();
        map.put("IP", IP.replaceAll("\\.", "_"));

        List list = providerInterfaceService.queryBaseInfoFromKPIDETAIL(map);
        if (list.isEmpty()) {
            return null;
        }
        String ext_unitId = null;
        for (Object o : list) {
            Map<String, String> ext_unit_map = (Map<String, String>) o;
            ext_unitId = ext_unit_map.get("EXT_UNIT_ID");
            break;

        }
        map.put("EXT_UNIT_ID", ext_unitId);
        list = providerInterfaceService.getExtUnitIdInfo(map);
        if (list.isEmpty()) {
            return null;
        }

        String kbp = "", device_name = "",device_id="",devtype="",manufacturer="";
        Map baseInfoJson = new HashMap<String,String>();
        for (Object o : list) {
            Map<String, String> kbpINfos = (Map<String, String>) o;
            kbp = kbpINfos.get("KBP_CLASS");
            device_name = kbpINfos.get("DEVICE_NAME");
            device_id = kbpINfos.get("DEVICE_ID");
            devtype = kbpINfos.get("DEVTYPE");
            manufacturer = kbpINfos.get("MANUFACTURER");
            baseInfoJson.put("DEV_NAME",device_name);
            baseInfoJson.put("DEVICE_ID",device_id);
            baseInfoJson.put("DEVTYPE",devtype);
            baseInfoJson.put("IP",IP);
            baseInfoJson.put("MANUFACTURER",manufacturer);
            break;
        }
        String kbp_class1="",kbp_class2="",kbp_class3="";
        if(StringUtils.isNotEmpty(kbp)){
            kbp_class1 = kbp.substring(0,2);
            kbp_class2 = kbp.substring(0,5);
            kbp_class3 = kbp.substring(0,8);

            map.put("KBP_CLASS1",kbp_class1);
            map.put("KBP_CLASS2",kbp_class2);
            map.put("KBP_CLASS3",kbp_class3);
        }

        list = providerInterfaceService.queryKpiList(map);
        if (list.isEmpty()) {
            return null;
        }

        List kpiList = new ArrayList();
        for (Object o : list) {
            Map<String, String> kpiInfo = (Map<String, String>) o;
            Map kpiListJson = new HashMap<String,String>();
            String kpi_id = kpiInfo.get("KPI_ID");
            String  kpi_name = kpiInfo.get("KPI_NAME");
            String kpi_desc = kpiInfo.get("KPI_DESC");
            kpiListJson.put("KPI_ID",kpi_id);
            kpiListJson.put("KPI_NAME",kpi_name);
            kpiListJson.put("KPI_DESC",kpi_desc);
            kpiList.add(kpiListJson);

        }
        Map jsonList= new HashMap();
        jsonList.put("baseinfo",baseInfoJson);
        jsonList.put("kpilist",kpiList);

        return jsonList;
    }


    /***
     * @Author zhangli
     * @Description //TODO  直接获取ip所指定时间段的所有指标值
     * @Date 17:09 2019/4/13
     * @Param [] startTime,endTime,EXT_UNIT_ID,kpi_id
     * @return java.lang.Object
     **/
    @ResponseBody
    @RequestMapping(value = "/queryKpiDataFromInfo", method = RequestMethod.POST)
    public Object queryKpiDataFromInfo(@RequestBody String requestBody) throws Exception {
        //验证 是否有权限
        if(visit(request).equals("0000")){
            JSONObject json = json = new JSONObject();
            json.put("error","unauthorized");
            json.put("error_description","Full unauthorized is required  to assess this resource");
            ajaxJson.setSuccess(false);
            ajaxJson.setMsg("验证失败");
            ajaxJson.setResult(json);
            return ajaxJson;
        }

        JSONObject jsonObject = JSONObject.fromObject(requestBody);
        Map<String, Object> param = this.getRequestMapSingle();
        String ext_unitId = String.valueOf(jsonObject.get("EXT_UNIT_ID"));

        String startTime = String.valueOf(jsonObject.get("startTime"));
        String endTime = String.valueOf(jsonObject.get("endTime"));
        String kpi_id = String.valueOf(jsonObject.get("kpi_id"));

        if (null == ext_unitId || StringUtils.isBlank(ext_unitId)) {
            return null;
        }

        Map map = new HashMap();
        map.put("EXT_UNIT_ID", ext_unitId);
        List list = providerInterfaceService.getExtUnitIdInfo(map);
        if (list.isEmpty()) {
            return null;
        }

        String kbp = "", device_name = "",device_id="",devtype="",manufacturer="",ip_addr="";
        Map baseInfoJson = new HashMap<String,String>();
        for (Object o : list) {
            Map<String, String> kbpINfos = (Map<String, String>) o;
            kbp = kbpINfos.get("KBP_CLASS");
            device_name = kbpINfos.get("DEVICE_NAME");
            device_id = kbpINfos.get("DEVICE_ID");
            devtype = kbpINfos.get("DEVTYPE");
            manufacturer = kbpINfos.get("MANUFACTURER");
            ip_addr = kbpINfos.get("IP_ADDR");
            baseInfoJson.put("DEV_NAME",device_name);
            baseInfoJson.put("DEVICE_ID",device_id);
            baseInfoJson.put("DEVTYPE",devtype);
            baseInfoJson.put("IP",ip_addr);
            baseInfoJson.put("MANUFACTURER",manufacturer);
            break;
        }
        String kbp_class1="",kbp_class2="",kbp_class3="";
        if(StringUtils.isNotEmpty(kbp)){
            kbp_class1 = kbp.substring(0,2);
            kbp_class2 = kbp.substring(0,5);
            kbp_class3 = kbp.substring(0,8);

            map.put("KBP_CLASS1",kbp_class1);
            map.put("KBP_CLASS2",kbp_class2);
            map.put("KBP_CLASS3",kbp_class3);
        }

        map.put("startTime",startTime);
        map.put("endTime",endTime);
        map.put("kpiId",kpi_id);

        list = providerInterfaceService.queryInfoFromHisData(map);
        if (list.isEmpty()) {
            return null;
        }

        List kpiList = new ArrayList();
        for (Object o : list) {
            Map<String, Object> kpiInfo = (Map<String, Object>) o;
            Map kpiListJson = new HashMap<String,String>();
            String _kpi_id =  String.valueOf(kpiInfo.get("KPI_ID"));
            String _unit_id =  String.valueOf(kpiInfo.get("UNIT_ID"));

            Timestamp _cll_time = (Timestamp) kpiInfo.get("CLL_TIME");
            Timestamp _db_time = (Timestamp) kpiInfo.get("DB_TIME");

            String _kpi_value = String.valueOf(kpiInfo.get("KPI_VALUE"));

            kpiListJson.put("UNIT_ID",_unit_id);
            kpiListJson.put("KPI_ID",_kpi_id);
            kpiListJson.put("KPI_VALUE",_kpi_value);
            kpiListJson.put("CLL_TIME",_cll_time);
            kpiListJson.put("DB_TIME",_db_time);

            kpiList.add(kpiListJson);

        }

        Map jsonList= new HashMap();
        jsonList.put("baseinfo",baseInfoJson);
        jsonList.put("kpiinfos",kpiList);

        return jsonList;

    }
    @ResponseBody
    @RequestMapping(value = "/getExtUnitIdInfo", method = RequestMethod.GET)
    public String getExtUnitIdInfo()throws Exception{
        Map map = new HashMap();
        map.put("IP", "10_230_2_63");
        List list = providerInterfaceService.getExtUnitIdInfo(map);

        System.out.println("a");
        System.out.println(list);
        return list.toString();

    }
    /***
     * @Author zhangli
     * @Description //TODO   直接获取ip所指定时间段的所有指标值
     * @Date 17:09 2019/4/13
     * @Param [] startTime,endTime,IP
     * @return java.lang.Object
     **/
    @ResponseBody
    @RequestMapping(value = "/queryAllKpiDataFromIP", method = RequestMethod.POST)
    public Object queryAllKpiDataFromIP(@RequestBody String requestBody) throws Exception {
        //验证 是否有权限
        if(visit(request).equals("0000")){
            JSONObject json = json = new JSONObject();
            json.put("error","unauthorized");
            json.put("error_description","Full unauthorized is required  to assess this resource");
            ajaxJson.setSuccess(false);
            ajaxJson.setMsg("验证失败");
            ajaxJson.setResult(json);
            return ajaxJson;
        }

        JSONObject jsonObject = JSONObject.fromObject(requestBody);

        String IP = String.valueOf(jsonObject.get("IP"));
        String startTime = String.valueOf(jsonObject.get("startTime"));
        String endTime = String.valueOf(jsonObject.get("endTime"));

        if (null == IP || StringUtils.isBlank(IP)) {
            return null;
        }
        if (null == IP || StringUtils.isBlank(IP)) {
            return null;
        }
        Map map = new HashMap();
        map.put("IP", IP.replaceAll("\\.", "_"));

        List list = providerInterfaceService.queryBaseInfoFromKPIDETAIL(map);
        if (list.isEmpty()) {
            return null;
        }
        String ext_unitId = null;
        for (Object o : list) {
            Map<String, String> ext_unit_map = (Map<String, String>) o;
            ext_unitId = ext_unit_map.get("EXT_UNIT_ID");
            break;

        }
        if (null == ext_unitId || StringUtils.isBlank(ext_unitId)) {
            return null;
        }
        map.put("EXT_UNIT_ID", ext_unitId);
        list = providerInterfaceService.getExtUnitIdInfo(map);
        if (list.isEmpty()) {
            return null;
        }

        String kbp = "", device_name = "",device_id="",devtype="",manufacturer="",ip_addr="";
        Map baseInfoJson = new HashMap<String,String>();
        for (Object o : list) {
            Map<String, String> kbpINfos = (Map<String, String>) o;
            kbp = kbpINfos.get("KBP_CLASS");
            device_name = kbpINfos.get("DEVICE_NAME");
            device_id = kbpINfos.get("DEVICE_ID");
            devtype = kbpINfos.get("DEVTYPE");
            manufacturer = kbpINfos.get("MANUFACTURER");
            ip_addr = kbpINfos.get("IP_ADDR");
            baseInfoJson.put("DEV_NAME",device_name);
            baseInfoJson.put("DEVICE_ID",device_id);
            baseInfoJson.put("DEVTYPE",devtype);
            baseInfoJson.put("IP",ip_addr);
            baseInfoJson.put("MANUFACTURER",manufacturer);
            break;
        }
        String kbp_class1="",kbp_class2="",kbp_class3="";
        if(StringUtils.isNotEmpty(kbp)){
            kbp_class1 = kbp.substring(0,2);
            kbp_class2 = kbp.substring(0,5);
            kbp_class3 = kbp.substring(0,8);

            map.put("KBP_CLASS1",kbp_class1);
            map.put("KBP_CLASS2",kbp_class2);
            map.put("KBP_CLASS3",kbp_class3);
        }

        map.put("startTime",startTime);
        map.put("endTime",endTime);

        list = providerInterfaceService.queryInfoFromHisData(map);
        if (list.isEmpty()) {
            return null;
        }

        List kpiList = new ArrayList();
        for (Object o : list) {
            Map<String, Object> kpiInfo = (Map<String, Object>) o;
            Map kpiListJson = new HashMap<String,String>();
            String _kpi_id =  String.valueOf(kpiInfo.get("KPI_ID"));
            String _unit_id =  String.valueOf(kpiInfo.get("UNIT_ID"));

            Timestamp _cll_time = (Timestamp) kpiInfo.get("CLL_TIME");
            Timestamp _db_time = (Timestamp) kpiInfo.get("DB_TIME");

            String _kpi_value = String.valueOf(kpiInfo.get("KPI_VALUE"));

            kpiListJson.put("UNIT_ID",_unit_id);
            kpiListJson.put("KPI_ID",_kpi_id);
            kpiListJson.put("KPI_VALUE",_kpi_value);
            kpiListJson.put("CLL_TIME",_cll_time);
            kpiListJson.put("DB_TIME",_db_time);

            kpiList.add(kpiListJson);

        }

        Map jsonList= new HashMap();
        jsonList.put("baseinfo",baseInfoJson);
        jsonList.put("kpiinfos",kpiList);

        return jsonList;

    }
    /***
     * @Author zhangli
     * @Description //TODO 授权验证：是否有权限获取API信息
     * @Date 15:58 2019/4/15
     * @Param [request]
     * @return net.sf.json.JSONObject
     **/
    public String visit(HttpServletRequest request) {
        if(request==null){
            return "0000";
        }
        Map<String,String > params = new HashMap<String,String>();
        Enumeration headerNames =  request.getHeaderNames();
        while(headerNames.hasMoreElements()){
            String key = (String) headerNames.nextElement();
            String value = request.getHeader(key);
            params.put(key,value);
        }

        String userId = params.get("userid");
        String passwd = params.get("passwd");
        Map<String,Object> _map = new HashMap<>();
        _map.put("loginCode",userId);
        _map.put("loginPasswd", DesEncrypterIsmp.encrypt(passwd));
        TbSysLoginmsg loginmsg = null;
        try {
            loginmsg = tbSysLoginmsgService.loginByParaMap(_map);
        } catch (Exception e) {
            e.printStackTrace();
        }
        if(loginmsg!=null){
            return "1111";
        }else {
            return "0000";
        }

    }


}
